﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Logic.Distribution;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities.XML;
using PPI.General;
using PPI.Mobile.General;

namespace PPI
{
    public partial class frmCopyMobileDatabase : frmBase, ICopyMobileDatabase
    {
        private string m_SelectPathToCopyDatabase;
        private bool m_CheckDistributionBDIntegrity;

        public EXmlOutboxBase SelectedOutbox
        {
            get { return m_SelectedOutbox; }
        }

        private EXmlOutboxBase m_SelectedOutbox;

        private CopyMobileDatabasePresenter m_presenter;

        public frmCopyMobileDatabase()
        {
            InitializeComponent();
            MenuMain.Visible = false;
            FormSize = FormSizes.Medium;
        }

        public frmCopyMobileDatabase(bool checkDistributionMobileDBIntegrity) : this()
        {
            m_CheckDistributionBDIntegrity = checkDistributionMobileDBIntegrity;
        }

        #region ICopyMobileDatabase

        public string SelectedPathToCopy
        {
            get { return m_SelectPathToCopyDatabase; }
        }

        public void ReloadOutboxFolders()
        {
            outboxDatabase1.LoadAllOutboxesToUpload();
        }

        public string ErrorMessage
        {
            set { MessageBox.Show(value); }
        }

        public int ProgressBarValue
        {
            set { pbCopyMobile.Value = value; }
        }

        public event EventHandler<EventArgs> StartCopy;
        
        #endregion

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbrDialog = new FolderBrowserDialog();
            if (fbrDialog.ShowDialog() == DialogResult.OK)
            {
                m_SelectPathToCopyDatabase = fbrDialog.SelectedPath;

                if (m_CheckDistributionBDIntegrity)
                {
                    DirectoryInfo _selectedDirectory = new DirectoryInfo(m_SelectPathToCopyDatabase);
                    string sdfFilePath = DirHelper.GetSdfPath(_selectedDirectory.FullName);

                    //Add necessary fields if missing
                    MobileGeneral.CreatePoleFields(sdfFilePath);
                    if (MobileGeneral.UpdateDistributionVersion(sdfFilePath)) 
                    {
                        LTop.UpdateData(sdfFilePath);
                        MobileGeneral.UpdateDBVersion(sdfFilePath);
                    }
                }

                if (StartCopy != null)
                    StartCopy(this, new EventArgs());
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if(outboxDatabase1.HasDatabases())
            {
                m_SelectedOutbox = outboxDatabase1.SelectedOutbox;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Ignore;
            this.Close();
        }

        private void frmCopyMobileDatabase_Load(object sender, EventArgs e)
        {
            try
            {
                m_presenter = new CopyMobileDatabasePresenter(this);
                ReloadOutboxFolders();
            }
            catch(Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

    }
}
